const fs = require('fs');
const path = require('path');

function copyDir(src, dest) {
  if (!fs.existsSync(src)) return;
  if (!fs.existsSync(dest)) {
    fs.mkdirSync(dest, { recursive: true });
  }

  for (const entry of fs.readdirSync(src)) {
    const srcPath = path.join(src, entry);
    const destPath = path.join(dest, entry);
    const stat = fs.statSync(srcPath);
    if (stat.isDirectory()) {
      copyDir(srcPath, destPath);
    } else {
      fs.copyFileSync(srcPath, destPath);
    }
  }
}

const projectRoot = __dirname;
const srcMigrations = path.join(projectRoot, 'src', 'migrations');
const distMigrations = path.join(projectRoot, 'dist', 'migrations');
const deployMigrations = path.join(projectRoot, 'deploy', 'migrations');

// 复制到 dist（给 start:prod 用）
copyDir(srcMigrations, distMigrations);

// 如果后续 build:standalone 生成了 deploy 目录，也把 migrations 一起拷贝过去
if (fs.existsSync(path.join(projectRoot, 'deploy'))) {
  copyDir(srcMigrations, deployMigrations);
}

console.log('Migrations copied (if any existed).');
